
<template>
  <div id="app">
    <h1>购物车</h1>
    <ul>
      <li style="list-style-type: none;" v-for="item in cart" :key="item.id">
        {{ item.name }} - ¥{{ item.price }} - 数量：{{ item.quantity }}
        <button @click="removeFromCart(item.id)">删除</button>
        <button @click="updateQuantity({ productId: item.id, quantity: item.quantity + 1 })">+</button>
        <button @click="updateQuantity({ productId: item.id, quantity: item.quantity - 1 })">-</button>
      </li>
    </ul>
    <p>总价：¥{{ totalPrice }}</p>
  </div>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'

export default {
  computed: {
    ...mapState(['cart']),
    ...mapGetters(['totalPrice'])
  },
  methods: {
    ...mapActions(['addToCart', 'removeFromCart', 'updateQuantity'])
  }
}
</script>
